<?php

App::uses('AppController', 'Controller');

/**
 * Places Controller
 *
 * @property Place $Place
 */
class PlacesController extends AppController {

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Place->recursive = 0;
        $this->set('places', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Place->id = $id;
        if (!$this->Place->exists()) {
            throw new NotFoundException(__('Invalid place'));
        }
        $this->set('place', $this->Place->read(null, $id));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Place->create();
            
            if ($this->Place->save($this->request->data)) {
                $this->Session->setFlash(__('The place has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The place could not be saved. Please, try again.'));
            }
        }
        $chapters = $this->Place->Chapter->find('list');
        $addresses = $this->Place->Address->find('list');
        $this->set(compact('chapters', 'addresses'));
    }

    /**
     * admin_edit method
     *
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Place->id = $id;
        if (!$this->Place->exists()) {
            throw new NotFoundException(__('Invalid place'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Place->save($this->request->data)) {
                $this->Session->setFlash(__('The place has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The place could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Place->read(null, $id);
        }
        $chapters = $this->Place->Chapter->find('list');
        $addresses = $this->Place->Address->find('list');
        $this->set(compact('chapters', 'addresses'));
    }

    /**
     * admin_delete method
     *
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Place->id = $id;
        if (!$this->Place->exists()) {
            throw new NotFoundException(__('Invalid place'));
        }
        if ($this->Place->delete()) {
            $this->Session->setFlash(__('Place deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Place was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    
    public function admin_find(){
        $term = (!empty($this->request->query['term'])) ? $this->request->query['term'] 
                : null;
        if(!$term){
            $this->set('result', array());
            return;
        }
        $places = $this->Place->find('all', array(
            'conditions' => array('Place.name LIKE'=>'%'.$term.'%'),
            'limit' => 20,
            'contain'   => array(
                'Address'
            ),
            'fields'    => array('Place.id','Place.name','Place.limit','Address.postal')
        ));
        $this->set('result', $places);
    }
    
    public function admin_get($id=null){
        if(!$id || !is_numeric($id)){
            $this->set('result', false);
            return;
        }
        
        $place = $this->Place->find('first', array(
            'conditions' => array('Place.id' => $id),
            'contain'   => array(
                'Address'
            )
        ));
        $this->set('result', $place);
    }
}